<template>
  <div ref="container" class="container">
    <div>
      <!-- hot -->
      <div class="hotCity">
        <div class="hot-title">
          热门城市
        </div>
        <div class="cities">
          <ul class="city-items">
            <li
              @click="changecitiesName(item.name)"
              v-for="item in hotCities"
              :key="item.id"
            >
              {{ item.name }}
            </li>
          </ul>
        </div>
      </div>
      <!-- 字母排序 -->
      <div class="hotCity">
        <div class="sort-title">
          字母排序
        </div>
        <div class="sort">
          <ul class="sort-items">
            <li
              v-for="(item, key) in sortCities"
              :key="key"
              @click="sortName(key)"
            >
              {{ key }}
            </li>
          </ul>
        </div>
      </div>
      <div
        class="listTitle"
        v-for="(item, key) in sortCities"
        :key="key"
        :ref="key"
      >
        <div class="list-title">
          {{ key }}
        </div>
        <div class="list">
          <ul class="list-items">
            <li
              @click="changecitiesName(item1.name)"
              v-for="item1 in item"
              :key="item1.id"
            >
              {{ item1.name }}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
// 引入better-scroll插件
import BScroll from 'better-scroll'
export default {
  props: ['sortCities', 'hotCities'],
  data() {
    return {
      scroll: ''
    }
  },
  methods: {
    ...mapMutations(['showCities']),
    sortName(SelectsortName) {
      this.$nextTick(() => {
        // 点击字母滚动到相应位置
        this.scroll.scrollToElement(this.$refs[SelectsortName][0])
      })
    },
    changecitiesName(name) {
      this.showCities(name)
      this.$router.push('/')
    }
  },
  mounted() {
    setTimeout(() => {
      const container = this.$refs.container
      this.scroll = new BScroll(container)
    }, 200)
  }
}
</script>

<style lang="less" scoped>
.container {
  position: absolute;
  left: 0;
  right: 0;
  top: 0.86rem;
  bottom: 0;
  overflow: hidden;
}
.sort-title {
  height: 0.7rem;
  line-height: 0.7rem;
  font-size: 0.34rem;
  font-weight: 600;
  padding-left: 0.3rem;
  padding: 0.2rem;
}
.sort-items {
  overflow: hidden;
  background-color: white;
  li {
    position: relative;
    height: 1rem;
    width: 16.66%;
    text-align: center;
    line-height: 1rem;
    font-size: 0.4rem;
    float: left;
  }
}
.list-title {
  height: 0.7rem;
  line-height: 0.7rem;
  font-size: 0.34rem;
  font-weight: 600;
  padding-left: 0.3rem;
  padding: 0.2rem;
}
.list-items {
  overflow: hidden;
  background-color: white;
  li {
    position: relative;
    height: 1rem;
    width: 25%;
    text-align: center;
    line-height: 1rem;
    font-size: 0.4rem;
    float: left;
    border-bottom: 0.02rem solid #ddd;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}
.list-items::before {
  content: '';
  height: 100%;
  width: 25%;
  left: 25%;
  position: absolute;
  border-left: 0.02rem solid #ddd;
  border-right: 0.02rem solid #ddd;
}
.list-items::after {
  content: '';
  height: 100%;
  width: 0;
  left: 75%;
  position: absolute;
  border-left: 0.02rem solid #ddd;
  border-right: 0.02rem solid #ddd;
}
.hot-title {
  height: 0.7rem;
  line-height: 0.7rem;
  font-size: 0.34rem;
  font-weight: 600;
  padding-left: 0.3rem;
  padding: 0.2rem;
}
.city-items {
  position: relative;
  overflow: hidden;
  background-color: white;
  li {
    position: relative;
    height: 1rem;
    width: 33.33%;
    text-align: center;
    line-height: 1rem;
    font-size: 0.4rem;
    float: left;
    border-bottom: 0.02rem solid #ddd;
  }
}
.city-items::before {
  content: '';
  position: absolute;
  height: 100%;
  width: 33.33%;
  left: 33.33%;
  border-left: 0.02rem solid #ddd;
  border-right: 0.02rem solid #ddd;
}
</style>
